﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Text;
using System.Threading.Tasks;
using JX.Application;
using JX.Core.Entity;
using JX.Infrastructure;
using JX.Infrastructure.Common;
using JX.Infrastructure.Framework.Authorize;
using JXWebSite.Areas.Admin.Models.PlusViewModels;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Primitives;
using MimeKit;

namespace JXWebSite.Areas.Admin.Controllers
{
	/// <summary>
	/// 附件管理控制器
	/// </summary>
	[Area("Admin")]
	[Route("Admin/[controller]/[action]")]
	[AdminAuthorize]
	public class PlusController : Controller
    {
		private IUserMessageServiceAppDapper _UserMessageServiceApp;
		private IUsersServiceAppDapper _UsersServiceApp;
		private IRegionServiceAppDapper _RegionServiceApp;
		private IDictionaryServiceAppDapper _DictionaryServiceApp;
		/// <summary>
		/// 依赖注入
		/// </summary>
		/// <param name="UserMessageServiceApp"></param>
		/// <param name="UsersServiceApp"></param>
		/// <param name="RegionServiceApp"></param>
		/// <param name="DictionaryServiceApp"></param>
		public PlusController(IUserMessageServiceAppDapper UserMessageServiceApp,
			IUsersServiceAppDapper UsersServiceApp,
			IRegionServiceAppDapper RegionServiceApp,
			IDictionaryServiceAppDapper DictionaryServiceApp)
		{
			_UserMessageServiceApp = UserMessageServiceApp;
			_UsersServiceApp = UsersServiceApp;
			_RegionServiceApp = RegionServiceApp;
			_DictionaryServiceApp = DictionaryServiceApp;
		}

		#region 站内信管理
		/// <summary>
		/// 站内信管理页面
		/// </summary>
		/// <returns></returns>
		[AdminAuthorize(Roles = "SuperAdmin,MessageManage")]
		public ActionResult MessageManage()
        {
            return View();
        }
		/// <summary>
		/// 得到站内信分页数据
		/// </summary>
		/// <returns></returns>
		[AdminAuthorize(Roles = "SuperAdmin,MessageManage")]
		public IActionResult GetMessageList()
		{
			int PageNum = Utility.Query("PageNum", 1);
			int PageSize = Utility.Query("PageSize", 10);
			string SearchName = Utility.Query("SearchName");
			string SearchKeyword = Utility.Query("SearchKeyword");
			int TabStatus = Utility.Query("TabStatus", -1);
			string filter = " 1=1 ";
			if (!string.IsNullOrEmpty(SearchKeyword))
			{
				filter += " and " + SearchName + " like '%" + DataSecurity.FilterBadChar(SearchKeyword) + "%'";
			}
			if (TabStatus >= 0)
			{
				filter += " and IsRead = " + TabStatus;
			}
			string strColumn = "MessageID,Title,Sender,Incept,SendTime,IsSend,IsDelInbox,IsDelSendbox,IsRead";
			int RecordTotal;
			var result = _UserMessageServiceApp.GetListPaged(startRowIndexId:(PageNum - 1) * PageSize,maxNumberRows:PageSize,
				StrColumn: strColumn, TableName: "", Filter: filter, 
				SortColumn: "MessageID", Sorts: "desc", out RecordTotal).ToList();
			PagerModel<UserMessageEntity> pagerModel = new PagerModel<UserMessageEntity>(PageNum, PageSize, RecordTotal, result);
			return Json(pagerModel);
		}
		/// <summary>
		/// 删除站内信
		/// </summary>
		/// <param name="id"></param>
		/// <returns></returns>
		[HttpPost]
		[ValidateAntiForgeryToken]
		[AdminAuthorize(Roles = "SuperAdmin,MessageManage")]
		public IActionResult DelMessage(int id)
		{
			if (id <= 0)
			{
				return Json(new
				{
					Result = "删除失败！没有指定要删除的记录ID！"
				});
			}
			try
			{
				if (_UserMessageServiceApp.Delete(p=>p.MessageID==id))
				{
					return Json(new
					{
						Result = "ok"
					});
				}
				else
				{
					return Json(new
					{
						Result = "删除失败！"
					});
				}
			}
			catch (Exception ex)
			{
				return Json(new
				{
					Result = "删除失败！" + ex.Message
				});
			}
		}
		/// <summary>
		/// 批量删除站内信
		/// </summary>
		/// <param name="ids"></param>
		/// <returns></returns>
		[HttpPost]
		[ValidateAntiForgeryToken]
		[AdminAuthorize(Roles = "SuperAdmin,MessageManage")]
		public IActionResult DelMessageMulti(string ids)
		{
			if (string.IsNullOrEmpty(ids))
			{
				return Json(new
				{
					Result = "删除失败！没有指定要删除的记录ID！"
				});
			}
			if (!DataValidator.IsValidId(ids))
			{
				return Json(new
				{
					Result = "删除失败！指定要删除的记录ID格式不对！"
				});
			}
			try
			{
				var arrIDs = StringHelper.GetArrayBySplit<int?>(ids).ToArray();
				_UserMessageServiceApp.Delete(p => arrIDs.Contains(p.MessageID));
				return Json(new
				{
					Result = "ok"
				});
			}
			catch (Exception ex)
			{
				return Json(new
				{
					Result = "删除失败！" + ex.Message
				});
			}
		}
		/// <summary>
		/// 批量删除站内信
		/// </summary>
		/// <param name="DelType"></param>
		/// <param name="DelValue"></param>
		/// <returns></returns>
		[HttpPost]
		[ValidateAntiForgeryToken]
		[AdminAuthorize(Roles = "SuperAdmin,MessageManage")]
		public IActionResult DelMessageBatch(int DelType=0, string DelValue="")
		{
			if (string.IsNullOrEmpty(DelValue))
			{
				return Json(new
				{
					Result = "删除失败！请指定要删除的发件人或日期！"
				});
			}
			try
			{
				bool bFlag = false;
				if (DelType == 0)
				{
					var arrDelValue = StringHelper.GetArrayBySplit<string>(DelValue).ToArray();
					bFlag = _UserMessageServiceApp.Delete(p=>arrDelValue.Contains(p.Sender));
				}
				else if (DelType == 1)
				{
					switch (DelValue)
					{
						case "0":
							bFlag = _UserMessageServiceApp.Delete(p=>true);
							break;
						default:
							bFlag = _UserMessageServiceApp.Delete(p => LambdaToSqlHelper.DateDiff("d",p.SendTime, DateTime.Now) >= DelValue.ToInt32(7));
							break;
					}
				}
				if (bFlag)
				{
					return Json(new
					{
						Result = "ok"
					});
				}
				else
				{
					return Json(new
					{
						Result = "删除失败！"
					});
				}
			}
			catch (Exception ex)
			{
				return Json(new
				{
					Result = "删除失败！" + ex.Message
				});
			}
		}
		/// <summary>
		/// 发送站内信页面
		/// </summary>
		/// <param name="id"></param>
		/// <param name="atype"></param>
		/// <returns></returns>
		[AdminAuthorize(Roles = "SuperAdmin,MessageManage")]
		public ActionResult MessageSend(int id=0,string atype="")
		{
			var viewModel = new MessageViewModel();
			viewModel.MessageID = id;
			if (string.IsNullOrEmpty(atype))
			{
				ViewBag.Incept = Utility.Query("UserName");
				viewModel.InceptType = 1;
				viewModel.Sender = User.FindFirst(ClaimTypes.Name).Value;
			}
			else
			{
				StringBuilder builder = new StringBuilder();
				var entity = _UserMessageServiceApp.Get(p => p.MessageID == id);
				viewModel.MessageID = (int)entity.MessageID;
				viewModel.Sender = User.FindFirst(ClaimTypes.Name).Value;
				switch (atype)
				{
					case "Reply":
						viewModel.Title = "Re: " + entity.Title;
						builder.Append("======在 ");
						builder.Append(entity.SendTime.Value.ToLongTimeString());
						builder.Append(" 您来信中写道：======\r\n");
						builder.Append(entity.Content);
						builder.Append("\r\n================================================\r\n");
						viewModel.Content = builder.ToString();
						viewModel.Incept = entity.Sender;
						viewModel.InceptType = 1;
						break;
					case "Forward":
						viewModel.Title = "Fw: " + entity.Title;
						builder.Append("============== 下面是转发信息 ==============\r\n");
						builder.Append("原发件人：" + entity.Sender + "\r\n");
						builder.Append("原发件内容：\r\n");
						builder.Append(entity.Content);
						builder.Append("\r\n================================================\r\n");
						viewModel.Content = builder.ToString();
						viewModel.InceptType = 1;
						viewModel.Incept = "";
						break;
				}
				ViewBag.Incept = viewModel.Incept;
			}
			return View(viewModel);
		}
		/// <summary>
		/// 发送站内信
		/// </summary>
		/// <param name="viewModel"></param>
		/// <param name="collection"></param>
		/// <returns></returns>
		[HttpPost]
		[ValidateAntiForgeryToken]
		[AdminAuthorize(Roles = "SuperAdmin,MessageManage")]
		public async Task<ActionResult> MessageSend(MessageViewModel viewModel, IFormCollection collection)
		{
			#region 添加并发送
			StringBuilder sbIncept = new StringBuilder();
			switch(viewModel.InceptType)
			{
				case 0://所有会员
					var listAll = await _UsersServiceApp.GetAllDataAsync<UsersEntity, string>(predicate:p => true, selector:p => p.UserName);
					listAll.ToList().ForEach(item => {
						StringHelper.AppendString(sbIncept,item);
					});
					break;
				case 1://指定会员
					string Incept = collection["ctlSelectUser"];
					if (string.IsNullOrEmpty(Incept))
					{
						ModelState.AddModelError(string.Empty, "收件人不能为空");
						return View(viewModel);
					}
					var strArray = StringHelper.GetArrayBySplit<string>(Incept);
					strArray.ForEach(item=> {
						if (_UsersServiceApp.IsExist(p => p.UserName == item))
						{
							StringHelper.AppendString(sbIncept, item);
						}
					});
					break;
				case 2://指定会员组
					var InceptGroup = collection["InceptGroup"];
					if (string.IsNullOrEmpty(InceptGroup))
					{
						ModelState.AddModelError(string.Empty, "收件人会员组不能为空");
						return View(viewModel);
					}
					var listGroup = await _UsersServiceApp.GetListBySQLAsync<string>("select UserName from Users where GroupID in ("+ InceptGroup + ")");
					List<string> list = listGroup.ToList();
					list.ForEach(item => {
						StringHelper.AppendString(sbIncept, item);
					});
					break;
			}
			if (string.IsNullOrEmpty(sbIncept.ToString()))
			{
				ModelState.AddModelError(string.Empty, "收件人不存在");
				return View(viewModel);
			}
			foreach (string strItem in sbIncept.ToString().Split(new char[] { ',' }))
			{
				var entity = new UserMessageEntity();
				entity.Title = viewModel.Title;
				entity.Content = viewModel.Content;
				entity.Sender = viewModel.Sender;
				entity.Incept = strItem;
				entity.SendTime = DateTime.Now;
				entity.IsSend = 1;
				entity.IsRead = 0;
				entity.IsDelInbox = 0;
				entity.IsDelSendbox = 0;
				await _UserMessageServiceApp.AddAsync(entity);
			}
			#endregion
			viewModel.result = "ok";
			return View(viewModel);
		}
		/// <summary>
		/// 查看站内信
		/// </summary>
		/// <param name="id"></param>
		/// <returns></returns>
		[AdminAuthorize(Roles = "SuperAdmin,MessageManage")]
		public ActionResult ViewMessage(int id = 0)
		{
			var entity = _UserMessageServiceApp.Get(p => p.MessageID == id);
			if(entity != null)
			{
				var currentAdmin = User.FindFirst(ClaimTypes.Name).Value;
				if(entity.Incept == currentAdmin)
				{
					entity.IsRead = 1;
					_UserMessageServiceApp.Update(entity);
				}
			}
			return View(entity);
		}
		#endregion

		#region 邮件发送
		/// <summary>
		/// 邮件发送页面
		/// </summary>
		/// <returns></returns>
		[AdminAuthorize(Roles = "SuperAdmin,MailListSend")]
		public ActionResult MailListSend()
		{
			var viewModel = new MailViewModel();
			ViewBag.Content = "";
			ViewBag.Incept = Utility.Query("UserName");
			viewModel.InceptType = 1;
			return View(viewModel);
		}
		/// <summary>
		/// 邮件发送
		/// </summary>
		/// <param name="viewModel"></param>
		/// <param name="collection"></param>
		/// <returns></returns>
		[HttpPost]
		[ValidateAntiForgeryToken]
		[AdminAuthorize(Roles = "SuperAdmin,MailListSend")]
		public async Task<ActionResult> MailListSend(MailViewModel viewModel, IFormCollection collection)
		{
			string content = collection["ctlContent"];
			ViewBag.Content = content;
			if (string.IsNullOrEmpty(content))
			{
				ModelState.AddModelError(string.Empty, "邮件内容不能为空");
				return View(viewModel);
			}
			content = content.Replace("src=\"", "src=\"" + Utility.GetApplicationName());

			StringBuilder sbIncept = new StringBuilder();
			switch (viewModel.InceptType)
			{
				case 0://所有会员
					var listAll = await _UsersServiceApp.GetAllDataAsync<UsersEntity, string>(predicate: p => true, selector: p => p.Email);
					listAll.ToList().ForEach(item => {
						StringHelper.AppendString(sbIncept, item);
					});
					break;
				case 1://指定会员
					string Incept = collection["ctlSelectUser"];
					if (string.IsNullOrEmpty(Incept))
					{
						ModelState.AddModelError(string.Empty, "收件人不能为空");
						return View(viewModel);
					}
					var arrIncept = StringHelper.GetArrayBySplit<string>(Incept).ToArray();
					var listIncept = await _UsersServiceApp.GetAllDataAsync<UsersEntity, string>(predicate: p => arrIncept.Contains(p.UserName), selector: p => p.Email);
					listIncept.ToList().ForEach(item => {
						StringHelper.AppendString(sbIncept, item);
					});
					break;
				case 2://指定会员组
					var InceptGroup = collection["InceptGroup"];
					if (string.IsNullOrEmpty(InceptGroup))
					{
						ModelState.AddModelError(string.Empty, "收件人会员组不能为空");
						return View(viewModel);
					}
					var arrInceptGroup = StringHelper.GetArrayBySplit<int?>(InceptGroup).ToArray();
					var listInceptGroup = await _UsersServiceApp.GetAllDataAsync<UsersEntity, string>(predicate: p => arrInceptGroup.Contains(p.GroupID), selector: p => p.Email);
					listInceptGroup.ToList().ForEach(item => {
						StringHelper.AppendString(sbIncept, item);
					});
					break;
				case 3:// 指定email
					if (string.IsNullOrEmpty(viewModel.InceptEmail))
					{
						ModelState.AddModelError(string.Empty, "指定收件人邮箱不能为空");
						return View(viewModel);
					}
					StringHelper.AppendString(sbIncept, viewModel.InceptEmail.TrimEnd(','));
					break;
			}
			if (string.IsNullOrEmpty(sbIncept.ToString()))
			{
				ModelState.AddModelError(string.Empty, "收件人邮箱不存在");
				return View(viewModel);
			}
			
			
			int SuccessCount = 0;
			int failCount = 0;
			StringBuilder sbFail = new StringBuilder();
			foreach (string strItem in sbIncept.ToString().Split(new char[] { ',' }))
			{
				MailSender sender2 = new MailSender();
				sender2.Subject = viewModel.Title;
				sender2.MailBody = content;
				sender2.IsBodyHtml = true;
				sender2.FromName = viewModel.Sender;
				//sender2.MailToAddressList.Add(new MailboxAddress(strItem));
				sender2.MailToAddressList.Add(MailboxAddress.Parse(strItem));
				if (sender2.Send() == MailState.Ok)
				{
					SuccessCount = SuccessCount + 1;
				}
				else
				{
					failCount = failCount + 1;
					StringHelper.AppendString(sbFail, strItem);
				}
			}
			var msg = "<li>发送完成！成功：<font color='blue'>" + SuccessCount.ToString() + "</fong>个，失败：<font color='red'>" + failCount.ToString()+ "</fong>个</li>";
			if(failCount > 0)
			{
				msg += "<li>失败email：" + sbFail.ToString() + "</li>";
			}
			Utility.WriteMessage(msg);
			return View(viewModel);
		}
		#endregion

		#region 数据字典管理
		/// <summary>
		/// 数据字典管理页面
		/// </summary>
		/// <param name="TableName"></param>
		/// <param name="FieldName"></param>
		/// <returns></returns>
		[AdminAuthorize(Roles = "SuperAdmin,ChoicesetManage")]
		public ActionResult ChoicesetManage(string TableName= "Author", string FieldName= "Type")
		{
			if (string.IsNullOrWhiteSpace(TableName))
			{
				TableName = "Contacter";
			}
			if (string.IsNullOrWhiteSpace(FieldName))
			{
				FieldName = "Income";
			}
			var result = _DictionaryServiceApp.Get(p => p.TableName == TableName && p.FieldName == FieldName);
			if (result == null)
			{
				result = new DictionaryEntity();
			}
			return View(result);
		}
		/// <summary>
		/// 数据字典管理
		/// </summary>
		/// <param name="model"></param>
		/// <param name="collection"></param>
		/// <returns></returns>
		[HttpPost]
		[ValidateAntiForgeryToken]
		[AdminAuthorize(Roles = "SuperAdmin,ChoicesetManage")]
		public ActionResult ChoicesetManage(DictionaryEntity model = null, IFormCollection collection = null)
		{
			if (model==null)
			{
				Utility.WriteMessage("请求参数错误", "mClose");
			}
			if (model.FieldID <= 0)
			{
				Utility.WriteMessage("请求参数错误", "mClose");
			}
			if (string.IsNullOrWhiteSpace(model.TableName))
			{
				Utility.WriteMessage("指定的字段不存在", "mClose");
			}
			if (string.IsNullOrWhiteSpace(model.FieldName))
			{
				Utility.WriteMessage("指定的字段不存在", "mClose");
			}
			model = _DictionaryServiceApp.Get(p => p.TableName == model.TableName && p.FieldName == model.FieldName);
			if (model == null)
			{
				Utility.WriteMessage("指定的字段不存在", "mClose");
			}
			StringBuilder builder = new StringBuilder();
			var radValue = DataConverter.CLng(collection["rad"]);
			var arrValue = model.FieldValue.Split('$', StringSplitOptions.RemoveEmptyEntries);
			for (var i = 0; i < arrValue.Length + 3; i++)
			{
				var txtID = "txt" + i;
				var txtValue = DataConverter.ToString(collection[txtID]);
				var chkID = "chk" + i;
				var chkValue = 0;
				if (collection[chkID] != StringValues.Empty)
				{
					chkValue = 1;
				}
				if (!string.IsNullOrWhiteSpace(txtValue))
				{
					builder.Append(txtValue);
					builder.Append("|");
					builder.Append(chkValue);
					builder.Append("|");
					if (radValue==i)
					{
						builder.Append("1");
					}
					else
					{
						builder.Append("0");
					}
					builder.Append("$");
				}
			}
			model.FieldValue = builder.ToString().TrimEnd('$');
			var returnUrl = string.Format("/admin/Plus/ChoicesetManage?TableName={0}&FieldName={1}", model.TableName, model.FieldName);
			if (_DictionaryServiceApp.Update(model))
			{
				Utility.WriteMessage("保存成功", returnUrl);
			}
			else
			{
				Utility.WriteMessage("保存失败", returnUrl);
			}
			return View(model);
		}
		/// <summary>
		/// 得到数据字典列表
		/// </summary>
		/// <returns></returns>
		[AdminAuthorize(Roles = "SuperAdmin,ChoicesetManage")]
		public IActionResult GetChoicesetList()
		{
			var result = _DictionaryServiceApp.GetList(p=>true,p=>p.OrderBy(item=>item.TableName)).ToList();
			return Json(result);
		}
		/// <summary>
		/// 更新数据字典
		/// </summary>
		/// <param name="FieldID"></param>
		/// <param name="FieldTitle"></param>
		/// <returns></returns>
		[HttpPost]
		[ValidateAntiForgeryToken]
		[AdminAuthorize(Roles = "SuperAdmin,ChoicesetManage")]
		public IActionResult UpdateChoicesetFieldName(int FieldID,string FieldTitle)
		{
			var entity = _DictionaryServiceApp.Get(FieldID);
			if (entity == null)
			{
				return Json(new
				{
					Result = "修改失败！没有找到指定的字段名！"
				});
			}
			entity.Title = FieldTitle;
			if(_DictionaryServiceApp.Update(entity))
			{
				return Json(new
				{
					Result = "ok"
				});
			}
			else
			{
				return Json(new
				{
					Result = "修改失败！"
				});
			}
		}
		#endregion

		#region 行政区划管理
		/// <summary>
		/// 行政区划管理页面
		/// </summary>
		/// <returns></returns>
		[AdminAuthorize(Roles = "SuperAdmin,RegionManage")]
		public IActionResult RegionManage()
		{
			return View();
		}
		/// <summary>
		/// 得到行政区划分页数据
		/// </summary>
		/// <returns></returns>
		[AdminAuthorize(Roles = "SuperAdmin,RegionManage")]
		public IActionResult GetRegionList()
		{
			int PageNum = Utility.Query("PageNum", 1);
			int PageSize = Utility.Query("PageSize", 15);
			string SearchName = Utility.Query("SearchName");
			string SearchKeyword = Utility.Query("SearchKeyword");
			int TabStatus = Utility.Query("TabStatus", 0);
			string filter = " 1=1 ";
			if (!string.IsNullOrEmpty(SearchKeyword))
			{
				filter += " and " + SearchName + " like '%" + DataSecurity.FilterBadChar(SearchKeyword) + "%'";
			}
			if (TabStatus > 0)
			{
				//filter += " and IsRead = " + TabStatus;
			}
			string strColumn = " * ";
			int RecordTotal;
			var result = _RegionServiceApp.GetListPaged(startRowIndexId: (PageNum - 1) * PageSize, maxNumberRows: PageSize,
				StrColumn: strColumn, TableName: "", Filter: filter,
				SortColumn: "RegionID", Sorts: "desc", out RecordTotal).ToList();
			PagerModel<RegionEntity> pagerModel = new PagerModel<RegionEntity>(PageNum, PageSize, RecordTotal, result);
			return Json(pagerModel);
		}
		/// <summary>
		/// 删除行政区划
		/// </summary>
		/// <param name="id"></param>
		/// <returns></returns>
		[HttpPost]
		[ValidateAntiForgeryToken]
		[AdminAuthorize(Roles = "SuperAdmin,RegionManage")]
		public IActionResult DelRegion(int id)
		{
			if (id <= 0)
			{
				return Json(new
				{
					Result = "删除失败！没有指定要删除的记录ID！"
				});
			}
			try
			{
				if (_RegionServiceApp.Delete(p => p.RegionID == id))
				{
					return Json(new
					{
						Result = "ok"
					});
				}
				else
				{
					return Json(new
					{
						Result = "删除失败！"
					});
				}
			}
			catch (Exception ex)
			{
				return Json(new
				{
					Result = "删除失败！" + ex.Message
				});
			}
		}
		/// <summary>
		/// 批量删除行政区划
		/// </summary>
		/// <param name="ids"></param>
		/// <returns></returns>
		[HttpPost]
		[ValidateAntiForgeryToken]
		[AdminAuthorize(Roles = "SuperAdmin,RegionManage")]
		public IActionResult DelRegionMulti(string ids)
		{
			if (string.IsNullOrEmpty(ids))
			{
				return Json(new
				{
					Result = "删除失败！没有指定要删除的记录ID！"
				});
			}
			if (!DataValidator.IsValidId(ids))
			{
				return Json(new
				{
					Result = "删除失败！指定要删除的记录ID格式不对！"
				});
			}
			try
			{
				var arrIDs = StringHelper.GetArrayBySplit<int?>(ids).ToArray();
				_RegionServiceApp.Delete(p => arrIDs.Contains(p.RegionID));
				return Json(new
				{
					Result = "ok"
				});
			}
			catch (Exception ex)
			{
				return Json(new
				{
					Result = "删除失败！" + ex.Message
				});
			}
		}
		#endregion

		#region 行政区划编辑
		/// <summary>
		/// 行政区划编辑页面
		/// </summary>
		/// <param name="id"></param>
		/// <returns></returns>
		[AdminAuthorize(Roles = "SuperAdmin,RegionManage")]
		public IActionResult RegionEdit(int id = 0)
		{
			var model = new RegionEntity();
			model.RegionID = id;
			if (id <= 0)
				return View(model);
			model = _RegionServiceApp.Get(p => p.RegionID == id);
			if (model == null || model.RegionID <= 0)
				return View(model);
			return View(model);
		}
		/// <summary>
		/// 行政区划编辑
		/// </summary>
		/// <param name="id"></param>
		/// <param name="model"></param>
		/// <param name="collection"></param>
		/// <returns></returns>
		[HttpPost]
		[ValidateAntiForgeryToken]
		[AdminAuthorize(Roles = "SuperAdmin,RegionManage")]
		public async Task<ActionResult> RegionEdit(int id = 0, RegionEntity model = null, IFormCollection collection = null)
		{
			if (string.IsNullOrEmpty(model.Country))
			{
				ModelState.AddModelError(string.Empty, "国家名称不能为空");
				return View(model);
			}
			if (string.IsNullOrEmpty(model.Province))
			{
				ModelState.AddModelError(string.Empty, "省份名称不能为空");
				return View(model);
			}
			if (string.IsNullOrEmpty(model.City))
			{
				ModelState.AddModelError(string.Empty, "城市名称不能为空");
				return View(model);
			}
			if (string.IsNullOrEmpty(model.PostCode))
			{
				ModelState.AddModelError(string.Empty, "邮政编码不能为空");
				return View(model);
			}
			if (string.IsNullOrEmpty(model.AreaCode))
			{
				ModelState.AddModelError(string.Empty, "区号不能为空");
				return View(model);
			}
			if (model.RegionID <= 0)
			{
				#region 添加
				if (!await _RegionServiceApp.AddAsync(model))
				{
					ModelState.AddModelError(string.Empty, "添加失败");
					return View(model);
				}
				id = (int)model.RegionID;
				#endregion
			}
			else
			{
				#region 修改
				if (!await _RegionServiceApp.UpdateAsync(model))
				{
					ModelState.AddModelError(string.Empty, "修改失败");
					return View(model);
				}
				#endregion
			}
			Utility.WriteMessage("操作成功", "mRefresh");
			return View(model);
		}
		/// <summary>
		/// 查看行政区划
		/// </summary>
		/// <param name="id"></param>
		/// <returns></returns>
		[AdminAuthorize(Roles = "SuperAdmin,RegionManage")]
		public IActionResult RegionView(int id = 0)
		{
			var model = new RegionEntity();
			model.RegionID = id;
			if (id <= 0)
			{
				Utility.WriteMessage("指定的ID不存在", "mClose");
				return View(model);
			}

			model = _RegionServiceApp.Get(p => p.RegionID == id);
			if (model == null || model.RegionID <= 0)
			{
				Utility.WriteMessage("指定的信息不存在", "mClose");
				return View(model);
			}
			return View(model);
		}
		#endregion
	}
}